package com.astudio.lhasa;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astudio.lhasa.model.City;
import com.astudio.lhasa.model.Image;
import com.astudio.lhasa.model.ImageDao;
import com.astudio.lhasa.model.TourSpot;
import com.astudio.lhasa.model.TourSpotDao;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.LinkedList;
import java.util.List;

public class CityDetail extends MainActivity {
    public final static String CITY_EXTRA = "city_info";
    private City city;
    private List<TourSpot> spots;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        city = getIntent().getParcelableExtra(CITY_EXTRA);

        if (spots == null) spots = MyApplication.getDaoSession()
                .getTourSpotDao()
                .queryBuilder()
                .where(TourSpotDao.Properties.City.eq(city.getId()))
                .list();

        setTitle(city.getName() + "景点");
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new RecyclerView.Adapter<VH>() {
            @NonNull
            @Override
            public VH onCreateViewHolder (@NonNull ViewGroup parent, int viewType){
                View v = LayoutInflater.from(CityDetail.this).inflate(R.layout.tour_spot_item, parent, false);
                return new VH(v);
            }

            @Override
            public void onBindViewHolder (@NonNull VH holder,int position){
                holder.bindData(spots.get(position));
            }

            @Override
            public int getItemCount () {
                return spots.size();
            }
        } ;
    }

    private class VH extends RecyclerView.ViewHolder {
        private View itemView;

        public VH(View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        public void bindData(final TourSpot spot) {
            SimpleDraweeView simpleDraweeView = itemView.findViewById(R.id.image);
            GenericDraweeHierarchyBuilder builder =
                    new GenericDraweeHierarchyBuilder(getResources());
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(300)
                    .setRoundingParams(RoundingParams.fromCornersRadius(14f))
                    .setPlaceholderImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                    .setPlaceholderImage(Integer.parseInt(city.getImages()), ScalingUtils.ScaleType.FIT_CENTER)
                    .build();
            simpleDraweeView.setHierarchy(hierarchy);
            List<Image> images = MyApplication
                    .getDaoSession()
                    .getImageDao()
                    .queryBuilder()
                    .where(ImageDao.Properties.Owner.eq(spot.getId()))
                    .limit(1).list();
            if (!images.isEmpty()) simpleDraweeView.setImageURI(images.get(0).getUrl());

            TextView nameTv = itemView.findViewById(R.id.name);
            if (TextUtils.isEmpty(spot.getNameEn())) {
                nameTv.setText(spot.getName());
            } else {
                nameTv.setText(spot.getName() + "(" + spot.getNameEn() + ")");
            }
            TextView summaryTv = itemView.findViewById(R.id.summary);
            summaryTv.setText(spot.getSummary().trim());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CityDetail.this, TourSpotDetailActivity.class);
                    intent.putExtra(TourSpotDetailActivity.TOUR_EXTRA, spot);
                    intent.putExtra(CITY_EXTRA, city);
                    startActivity(intent);
                }
            });
        }
    }
}
